package cn.starsray.resource;

import cn.starsray.common.Response;
import cn.starsray.constant.ResponseEnum;
import cn.starsray.entity.Employee;
import cn.starsray.repository.EmployeeRepository;
import io.quarkus.panache.common.Parameters;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.*;

@Path("j/employee")
@ApplicationScoped
public class EmployeeJPAResource {

    @Inject
    EmployeeRepository repository;

    /**
     * 保存
     *
     * @param employee employee
     * @return {@link Response<Boolean> }
     */
    @POST
    @Path("save")
    public Response<Boolean> save(Employee employee) {
        repository.persist(employee);
        return Response.succ(ResponseEnum.SUCC);
    }

    /**
     * 查询单个员工信息
     *
     * @param empNo empNo
     * @return {@link Response<Employee> }
     */
    @GET
    @Path("findById")
    public Response<Employee> findById(@QueryParam("empNo") int empNo) {
        Employee employee = repository.find("emp_no", empNo).firstResult();
        return Response.succ(ResponseEnum.SUCC, employee);
    }

    /**
     * 根据账号删除
     *
     * @param empNo 电磁脉冲号
     * @return {@link Response<Boolean> }
     */
    @DELETE
    @Path("deleteByNo")
    public Response<Boolean> deleteByNo(@QueryParam("empNo") int empNo) {
        boolean delete = repository.delete("emp_no", empNo) > 0;
        return Response.succ(ResponseEnum.SUCC, delete);
    }

    @PUT
    @Path("modify")
    public Response<Boolean> modify(Employee employee) {
        Parameters params = Parameters.with("emp_no", employee.getEmpNo())
                .and("birth_date", employee.getBirthDate())
                .and("first_name", employee.getFirstName())
                .and("last_name", employee.getLastName());
        boolean modify = repository.update("emp_no", params) > 0;
        return Response.succ(ResponseEnum.SUCC, modify);
    }


}
